package org.example.ajintegration.controller;

import org.example.ajintegration.proxy.AJReportProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/ajreport/test")
public class AJReportTestController {
    
    @Autowired
    private AJReportProxy ajReportProxy;
    
    /**
     * 测试 GET 请求
     */
    @GetMapping("/get")
    @ResponseBody
    public Map<String, Object> testGet() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 构建查询参数
            HashMap<String, String> params = new HashMap<>();
            params.put("page", "1");
            params.put("size", "10");
            
            // 调用 AJReport 的 GET 接口
            String response = ajReportProxy.proxiedGetMethod("/some/path", params);
            
            result.put("success", true);
            result.put("data", response);
            
        } catch (IOException e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 测试 POST 请求
     */
    @PostMapping("/post")
    @ResponseBody
    public Map<String, Object> testPost(@RequestBody Map<String, Object> requestBody) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 调用 AJReport 的 POST 接口
            String response = ajReportProxy.proxiedPostMethod("/some/post/path", 
                new HashMap<>(requestBody));
            
            result.put("success", true);
            result.put("data", response);
            
        } catch (IOException e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 手动更新 token
     */
    @PostMapping("/update-token")
    @ResponseBody
    public Map<String, Object> updateToken() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            ajReportProxy.updateToken();
            result.put("success", true);
            result.put("message", "Token 更新成功");
            result.put("token", ajReportProxy.getToken());
            
        } catch (IOException e) {
            result.put("success", false);
            result.put("error", e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 获取当前 token 信息
     */
    @GetMapping("/token-info")
    @ResponseBody
    public Map<String, Object> getTokenInfo() {
        Map<String, Object> result = new HashMap<>();
        result.put("token", ajReportProxy.getToken());
        result.put("expireTime", ajReportProxy.getTokenExpireTime());
        result.put("expireTimeFormatted", new java.util.Date(ajReportProxy.getTokenExpireTime()));
        
        long currentTime = System.currentTimeMillis();
        long timeUntilExpire = ajReportProxy.getTokenExpireTime() - currentTime;
        result.put("timeUntilExpireSeconds", timeUntilExpire / 1000);
        
        return result;
    }
}
